<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class Linklistcontroller extends Controller
{
    public function index () 
    {	

    	//实例化要操作的表
    	$db = DB::table('links');
    	// 分页
    	$list = $db->paginate(5);
    	//加载模板
    	return view('admin.link.linklist',['list'=>$list]);
    }
    public function create() 
    {
    	return view('admin.link.link');
    }
    public function store(Request $request)
    {
    	$messages = [
    		'required' => ':attribute 的字段是必要的'
    	];
    	//
    	$this->validate($request, [
	        'links_name' => 'required|unique:links|max:16',
	        'links_url' => 'required|unique:links',
	    ],$messages);
    	$name = $request->except('_token');
    	//dd($name);
    	$list = DB::table('links')->insertGetId($name);
    	 if($list>0) {
    	 	return redirect('admin/linklist')->with('msg','添加成功');
    	 }
    }
    public function destroy ($id)
    {

    	$list = DB::table('links')->where('links_id',$id)->delete();
    	if($list>0){
    		return redirect('admin/linklist');
    	}
    }
    public function edit($id)
    {
    	$list = DB::table('links')->where('links_id',$id)->first();
    	return view('admin.link.linkedit',['list'=>$list]);
    }

    public function update($id,Request $request)
    {
    	// dd($request);
        $messages = [
            'required' => ':attribute 的字段是必要的'
        ];
        //
        $this->validate($request, [
            'links_name' => 'required|unique:links|max:16',
        ],$messages);
    	$name = $request->except("_token","_method");
    	// dd($name);
    	$list = DB::table('links')->where('links_id',$id)->update($name);
    	if($list>0){
    		return redirect('admin/linklist');
    	}

    }
}
